package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.TPerson;
import com.dk.pojo.TRole;
import com.dk.service.TRoleService;
import com.dk.service.TpersonService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("TP")
public class TpersonController {
    @Reference//用这个注解来远程调用service而不是Autowired
    private TpersonService  tpersonService;
    @Reference//用这个注解来远程调用service而不是Autowired
    private TRoleService tRoleService;
    @RequestMapping("toShow")
    public String toShow(){

        return "user/show";
    }
    @RequestMapping("getInfo")
    @ResponseBody
    public List<TPerson> getInfo(){
        List<TPerson> list = tpersonService.getInfo();
        return list;
    }
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<TRole> list = tRoleService.getInfo();
        model.addAttribute("list",list);
        return "user/add";
    }
    @RequestMapping("addInfo")
    @ResponseBody
    public boolean addInfo(TPerson tPerson,@RequestParam("roleId") Integer[] roleId){
        boolean t = tpersonService.addInfo(tPerson,roleId);
        return t;
    }
    @RequestMapping("toUpdate/{id}")
    public String toUpdate(Model model,@PathVariable("id") Integer id){
        TPerson u = tpersonService.getinfoById(id);
        List<TRole> getinfo = tRoleService.getInfo();
        List<Integer> rids = tRoleService.getinfoByUid(id);
        for (int i = 0; i < getinfo.size(); i++) {
            if (rids.contains(getinfo.get(i))){
                boolean list = true;
                model.addAttribute("list",list);
            }else {
                boolean list = false;
                model.addAttribute("list",list);
            }
        }
        model.addAttribute("u",u);
        model.addAttribute("r",getinfo);
        return "user/update";
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(TPerson tPerson,@RequestParam("roleId") Integer[] roleId){
        boolean tr = tpersonService.updateInfo(tPerson,roleId);
        return tr;
    }

    @RequestMapping("deleteInfo/{id}")
    @ResponseBody
    public boolean deleteInfo(@PathVariable("id")Integer pid){
        boolean b = tpersonService.deleteId(pid);
        return b;
    }
}
